
import at.ac.tuwien.big.we.highscore.Failure;
import at.ac.tuwien.big.we.highscore.PublishHighScoreEndpoint;
import at.ac.tuwien.big.we.highscore.PublishHighScoreService;
import at.ac.tuwien.big.we.highscore.data.HighScoreRequestType;
import at.ac.tuwien.dbai.ssd.Game;
import at.ac.tuwien.dbai.ssd.InfoType;
import at.ac.tuwien.dbai.ssd.InfoType.Winner;
import ewa.big.entities.Player;
import java.io.StringWriter;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

/*
 * To change this template, choose Tools | Templates
 * and open the template highScoreRequest the editor.
 */

/**
 *
 * @author rvlcek
 */
public class TestClient {
    
    
       public static void main(String[] args) throws MalformedURLException, DatatypeConfigurationException, JAXBException, Failure {
           
/*
        PublishHighScoreService service = new PublishHighScoreService();  
            
        PublishHighScoreService service = new PublishHighScoreService(
                new URL ("http://localhost:8080/UE4/PublishHighScoreImplService?wsdl"),
                new QName ("http://localhost:8080/UE4/PublishHighScoreImplService", "PublicHighScoreService"));
  */
        PublishHighScoreService service = new PublishHighScoreService(
            new URL ("http://localhost:8080/UE4/highscore?wsdl"),
            new QName ("http://localhost:8080/UE4/highscore", "PublishHighScoreImpl"));   
        PublishHighScoreEndpoint endpoint = service.getPublishHighScorePort();
        
        HighScoreRequestType highScoreRequest = new HighScoreRequestType();
        
        highScoreRequest.setUserKey("G8N4P3E5D2S8Y4X2V9M5N");
        
        Game g = new Game();
        
        
        InfoType infoType = new InfoType();
        
        InfoType.Players players = new InfoType.Players();
        players.setNumber(2);
        
        InfoType.Players.Screenname screenName = new InfoType.Players.Screenname();
        screenName.setPlayer(2);
        screenName.setValue("String 1");
        
           
        players.getScreenname().add(screenName);
        
        
    //    DescType descType = new DescType();
    
        Player p = new Player();
        
        p.setFirstname("Hans");
        p.setLastname("Wurst");
        p.setUsername("Strizzi");
        
        
        Winner w = new Winner();
        w.setPlayer(1);

        XMLGregorianCalendar today = DatatypeFactory.newInstance().newXMLGregorianCalendar(
        new GregorianCalendar(2012,5,5));
       
       
      //  infoType.setDescription(descType);
        infoType.setName("Info Type Name");
        infoType.setPlayers(players);
        infoType.setRounds(BigInteger.valueOf(100L));
        infoType.setStarted(today);
        infoType.setWinner(w); 

        g.setInfo(infoType);

        
        
        highScoreRequest.setGame(g);
        
        String uuid;
        
       // System.out.println(toXML(highScoreRequest));
        try {
            uuid = endpoint.publishHighScore(highScoreRequest);
            System.out.println("UUID ist:  " + uuid);
        } catch (Failure ex) {
            Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    } 

       public static String toXML(HighScoreRequestType game) throws JAXBException{
                   final JAXBContext context = JAXBContext.newInstance(Game.class);
 
        // =============================================================================================================
        // Marshalling OBJECT to XML
        // =============================================================================================================
 
        // Create the marshaller, this is the nifty little thing that will actually transform the object into XML
        final Marshaller marshaller = context.createMarshaller();
        
        
        // Create a stringWriter to hold the XML
        final StringWriter stringWriter = new StringWriter();
 
        // Marshal the javaObject and write the XML to the stringWriter
        marshaller.marshal(game, stringWriter);
        
 
        // Print out the contents of the JavaObject we just unmarshalled from the XML
        return stringWriter.toString();
        
       }
}
